<template>
  <div class="background">
    <div class="login-form-layout">
      <el-form autocomplete="on" :model="loginForm" ref="loginForm" label-position="left" :rules="loginFormRules">
        <div class="login-text">欢迎登陆</div>
        <div class="login-input">
          <el-form-item class="input-item" prop="username">
            <el-input type="text" placeholder="请输入账号" v-model="loginForm.username" prefix-icon="el-icon-user-solid" />
          </el-form-item>
          <el-form-item prop="password" class="input-item">
            <el-input type="password" placeholder="请输入密码" v-model="loginForm.password" prefix-icon="el-icon-unlock"
              show-password @keyup.enter.native="onSubmit('loginForm')" />
          </el-form-item>
          <el-form-item class="input-item">
            <el-popover placement="bottom-start">
              <!-- 登录按钮 禁用或者不禁用 -->
              <el-button v-if="loginSubmit == false" type="primary" class="btn" v-on:click="onSubmit('loginForm')"
                slot="reference">
                <template v-if="loginSubmit == false">登录</template>
                <template v-else>
                  登录中
                  <i class="el-icon-loading"></i>
                </template>
              </el-button>
              <el-button v-else type="primary" v-on:click="onSubmit('loginForm')" slot="reference" disabled class="btn">
                <template v-if="loginSubmit == false">登录</template>
                <template v-else>
                  登录中
                  <i class="el-icon-loading"></i>
                </template>
              </el-button>
            </el-popover>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="el-login-footer">
      <!-- <span>Copyright © 2014-2020 cqbaotu.com All Rights Reserved.</span>
      <div class="default" @click="routeToDefault">切回大屏</div> -->
    </div>
  </div>
</template>

<script>
  import dragVerify from "vue-drag-verify";
  import login_center_bg from "@/assets/img/login/login_center_bg.png";

  export default {
    name: "Login",
    components: {
      dragVerify,
    },
    data() {
      //验证规则
      var valiUsername = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请输入账号！"));
        } else {
          if (value.length < 5 || value.length > 15) {
            callback(new Error("请确认位数！"));
          }
        }
        callback();
      };
      var valiPassword = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请输入密码！"));
        } else {
          callback();
        }
      };
      return {
        // verify: {
        //   //滑动验证组件
        //   handlerIcon: "fa fa-angle-double-right",
        //   successIcon: "fa fa-check",
        //   background: "#cccccc",
        //   progressBarBg: "#4b0",
        //   completedBg: "#66cc66",
        //   handlerBg: "#fff",
        //   text: "请滑动验证",
        //   successText: "验证成功,请点击登录",
        //   width: 320,
        //   height: 62,
        //   textSize: "18px",
        //   isCircle: "true"
        // },
        timer: false,
        screenWidth: document.body.clientWidth,
        verifyRefresh: true, //重置验证值
        loginForm: {
          //登录表单值
          username: "",
          password: "",
        },
        loginFormRules: {
          //登录验证
          username: [{
            validator: valiUsername,
            trigger: "blur",
          }, ],
          password: [{
            validator: valiPassword,
            trigger: "blur",
          }, ],
        },
        login_center_bg: login_center_bg, //登录背景图片
        loginSubmit: false, //登录状态
      };
    },
    methods: {
      routeToDefault() {
        this.$router.push({
          path: "/default",
        });
      },
      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // if (this.$refs.Verify.isPassing == false) {
            //   this.$message({
            //     message: "请滑动下方验证条！",
            //     type: "warning"
            //   });
            // } else {
            var that = this;
            this.loginSubmit = true;
            setTimeout(function () {
              that.$store
                .dispatch("setToken", that.loginForm)
                .then((data) => {
                  if (data.data.token) {
                    that.$router.push({
                      path: "/screen",
                    });
                  } else {
                    that.$message.error("用户名或者密码错误！");
                  }
                })
                .catch((res) => {
                  that.$message({
                    showClose: true,
                    message: res.data.respondMessage,
                    type: "error",
                  });
                });
              that.loginSubmit = false;
            }, "2000");
            //}
          } else {
            return false;
          }
        });
        this.verifyRefresh = false;
        this.$nextTick(() => {
          this.verifyRefresh = true;
        });
      },
    },
    mounted() {
      const _that = this;
      window.onresize = () => {
        return (() => {
          window.screenWidth = document.body.clientWidth;
          _that.screenWidth = window.screenWidth;
        })();
      };
    },
    watch: {
      screenWidth(val) {
        if (!this.timer) {
          this.screenWidth = val;
          this.timer = true;
          let _that = this;
          setTimeout(function () {
            _that.timer = false;
          }, 400);
        }
      },
    },
  };
</script>

<style>
  * {
    margin: 0;
    padding: 0;
  }

  .login-form-layout {
    position: absolute;
    top: 32%;
    left: 60%;
    margin: 0 auto;
    border: none;
    width: 15%;
  }

  .login-title {
    text-align: center;
    padding-bottom: 30px;
    padding-top: 30px;
    font-size: 30px;
    color: dodgerblue;
  }

  .background {
    justify-content: center;
    display: flex;
    height: 100%;
    width: 100%;
    background: url("~@/assets/img/login/bj.png");
    background-size: 100% 100%;
  }

  .el-popover {
    display: none;
  }

  .login-form-layout .el-form-item__content {
    margin-bottom: 10px;
  }

  .login-form-layout .el-input--prefix .el-input__inner {
    width: 100%;
    background: #F7F5FB;
    border: none;
  }

  .login-input {
    width: 100%;
    text-align: center;
  }

  .input-item {
    width: 100%;
  }

  @font-face {
    font-family: PangMenZhengDao;
    src: url("~@/util/PangMenZhengDaoBiaoTiTi-1.ttf");
  }

  .title-left {
    float: left;
    margin-left: 15%;
    margin-bottom: 21px;
    font-family: PangMenZhengDao;
    font-size: 35px;
    letter-spacing: 0;
  }

  .title-right {
    float: left;
    font-size: 15px;
    margin-left: 1%;
    margin-top: 17px;
    font-family: PangMenZhengDao;
  }

  .title-left1 {
    float: left;
    margin-left: 15%;
    margin-bottom: 21px;
    font-size: 40px;
    font-family: cursive;
    font-weight: bold;
  }

  .title-right1 {
    float: left;
    font-size: 15px;
    margin-left: 2%;
    margin-top: 17px;
    font-size: 20px;
    font-family: cursive;
    font-weight: bold;
  }

  .btn {
    background: -webkit-gradient(linear,
        100% 0%,
        0% 0%,
        from(rgb(25, 124, 223)),
        to(#82d0fd));
    color: #fff;
    font-size: 20px;
    width: 100%;
  }

  .el-button--primary .el-popover__reference {
    height: 45px;
  }

  .el-login-footer {
    position: absolute;
    bottom: 50px;
    color: #ffffff;
  }

  .default {
    border-bottom: 1px solid gray;
    float: left;
    margin-right: 10px;
  }

  .default:hover {
    cursor: pointer;
    color: #82d0fd;
    border-bottom: 1px solid #82d0fd;
  }

  .el-button--primary .el-popover__reference {
    height: 45px;
  }

  .login-text {
    font-size: 1rem;
    margin-bottom: 20px;
    text-align: center;
  }

  .login-form-layout .el-input--suffix .el-input__inner {
    padding-left: 30px;
  }

  .el-input--suffix .el-input__inner {
    font-size: 14px;
    padding-right: 15px !important;
  }
</style>